<?php
/**
 * 
 * Users model
 *
 * @category Abovo
 * 
 * @package Abovo_Model
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 */

/**
 * 
 * Users model
 *
 * @package Abovo
 * 
 * @package Abovo_Model
 * 
 */
class Abovo_Model_Contacts_Users extends Abovo_Model_Contacts {
    
    /**
     * 
     * Fetches all users
     * 
     * @return Solar_Sql_Rowset
     * s
     */
    public function fetchAll($where = null, $order = null, $page = null)
    {
        return parent::fetchAll('handle IS NOT NULL', $order, $page);
    }
    
    /**
     * 
     * Fetches contact information for user handle
     * 
     * @return Solar_Sql_Row|bool Return bool false if handle
     * doesn't exist or contacts_id is not set. Otherwise
     * a Solar_Sql_Row object with contact info.
     * 
     */
    public function fetchByHandle($handle)
    {
        $user = $this->select('row', array('handle = ?' => $handle));
        
        if (count($user) > 0) {
            return $user;
        }
        
        // handle does not exist!
        return false;
    }
    
    /**
     * 
     * Creates and fetches form hints for contact
     * 
     * @param bool $company Add company form elements or not?
     * 
     * @return Solar_Form
     * 
     */
    public function form($cols = array())
    {
        $form = Solar::factory('Abovo_Form');
        
        settype($cols, 'array');
        
        // if cols is empty then use this set of columns
        if (empty($cols)) {
            $cols = array(
                'firstname',
                'lastname',
                'gender',
                'email',
                'tel',
                'adr',
                'postal_code',
                'city',
                'country',
            );
        }
        
        // load form element from table object
        $form->load(
            'Abovo_Form_Load_Table',
            $this,
            $cols,
            'contact'
        );
        
        /*
        // change 'gender' element to select and set options
        $form->elements['contact[gender]']['type']    = 'select';
        $form->elements['contact[gender]']['options'] = array(
            1 => $this->locale('TEXT_GENDER_MALE'),
            0 => $this->locale('TEXT_GENDER_FEMALE'),
        );
        
        // change country element's type to 'select'
        $form->elements['contact[country]']['type'] = 'select';
        
        // get list of countrys as options
        $options = Solar::factory('Lux_Intl')->getCountryList();
        $form->elements['contact[country]']['options'] = $options;
        */
        
        // done.
        return $form;
    }
}
